# nodemon --watch fetcher/07assign.py --exec 'uv run fetcher/07assign.py'

""" html解析; 列表页->详情页下载 """

from lxml import etree
import requests
headers = {
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
    "accept-language": "en-US,en;q=0.9,zh;q=0.8,zh-CN;q=0.7",
    # "cache-control": "max-age=0",
    # "if-modified-since": "Fri, 12 Jan 2024 06:39:41 GMT",
    # "if-none-match": "W/\"8034df1e2245da1:0\"",
    "priority": "u=0, i",
    "sec-ch-ua": "\"Not)A;Brand\";v=\"8\", \"Chromium\";v=\"138\", \"Google Chrome\";v=\"138\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\"",
    "sec-fetch-dest": "document",
    "sec-fetch-mode": "navigate",
    "sec-fetch-site": "none",
    "sec-fetch-user": "?1",
    "upgrade-insecure-requests": "1",
    "cookie": "Hm_lvt_398913ed58c9e7dfe9695953fb7b6799=1754359871; HMACCOUNT=35E7BF0AF54669DE; _clck=ove18r%7C2%7Cfy7%7C0%7C2043; Hm_lpvt_398913ed58c9e7dfe9695953fb7b6799=1754360262; _clsk=8lgveq%7C1754360263335%7C4%7C1%7Ck.clarity.ms%2Fcollect"
}
url = 'https://sc.chinaz.com/jianli/free.html'
response = requests.get(url=url,headers=headers)
response.encoding = 'utf-8'
page_text = response.text
# print('page_text')

# with open('tmp.html','w' ,encoding='utf-8') as fp:
#     fp.write(page_text)
# print(page_text)
# exit(0)

#数据解析:简历名称+详情⻚的url
tree = etree.HTML(page_text)
div_list = tree.xpath('//*[@id="container"]/div')

from urllib.request import urlretrieve
for div in div_list:
    title = div.xpath('./p/a/text()')[0]+'.html'
    detail_url = div.xpath('./p/a/@href')[0]
    # print(title,detail_url)
    #对详情⻚的url发起请求
    detail_page_text = requests.get(url=detail_url,headers=headers).text
    # print(title,detail_page_text)
    # #数据解析：下载地址
    tree = etree.HTML(detail_page_text)
    download_url = tree.xpath('//*[@id="down"]/div[2]/ul/li[1]/a/@href')[0]
    # #在下载请求建⽴模板
    print(title,download_url)
    """ 下载有限制 """
    # data = requests.get(url=download_url,headers=headers).content
    
    # urlretrieve(url,title)
    # print(title)
    # with open(title,'wb') as fp:
    #   fp.write(data)
    # print(title,'保存下载成功！')
    # break 
    
    
    
    """ curl 测试 """
    # curl  -L https://downsc.chinaz.net/Files/DownLoad/jianli/202312/zjianli2326.rar -o test.rar -v
    
#  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
#                                  Dload  Upload   Total   Spent    Left  Speed
#   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Host downsc.chinaz.net:443 was resolved.
# * IPv6: (none)
# * IPv4: 221.5.46.134
# *   Trying 221.5.46.134:443...
# * Connected to downsc.chinaz.net (221.5.46.134) port 443
# * ALPN: curl offers h2,http/1.1
# } [5 bytes data]
# * TLSv1.3 (OUT), TLS handshake, Client hello (1):
# } [512 bytes data]
# *  CAfile: /etc/ssl/certs/ca-certificates.crt
# *  CApath: /etc/ssl/certs
# { [5 bytes data]
# * TLSv1.3 (IN), TLS handshake, Server hello (2):
# { [98 bytes data]
# * TLSv1.2 (IN), TLS handshake, Certificate (11):
# { [4714 bytes data]
# * TLSv1.2 (IN), TLS handshake, Server key exchange (12):
# { [300 bytes data]
# * TLSv1.2 (IN), TLS handshake, Server finished (14):
# { [4 bytes data]
# * TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
# } [37 bytes data]
# * TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
# } [1 bytes data]
# * TLSv1.2 (OUT), TLS handshake, Finished (20):
# } [16 bytes data]
#   0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* TLSv1.2 (IN), TLS handshake, Finished (20):
# { [16 bytes data]
# * SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384 / X25519 / rsaEncryption
# * ALPN: server accepted h2
# * Server certificate:
# *  subject: CN=*.chinaz.net
# *  start date: Nov 11 00:00:00 2024 GMT
# *  expire date: Nov 11 23:59:59 2025 GMT
# *  subjectAltName: host "downsc.chinaz.net" matched cert's "*.chinaz.net"
# *  issuer: C=CN; O=TrustAsia Technologies, Inc.; CN=TrustAsia RSA DV TLS CA G3
# *  SSL certificate verify ok.
# *   Certificate level 0: Public key type RSA (2048/112 Bits/secBits), signed using sha384WithRSAEncryption
# *   Certificate level 1: Public key type RSA (3072/128 Bits/secBits), signed using sha384WithRSAEncryption
# *   Certificate level 2: Public key type RSA (4096/152 Bits/secBits), signed using sha384WithRSAEncryption
# } [5 bytes data]
# * using HTTP/2
# * [HTTP/2] [1] OPENED stream for https://downsc.chinaz.net/Files/DownLoad/jianli/202312/zjianli2326.rar
# * [HTTP/2] [1] [:method: GET]
# * [HTTP/2] [1] [:scheme: https]
# * [HTTP/2] [1] [:authority: downsc.chinaz.net]
# * [HTTP/2] [1] [:path: /Files/DownLoad/jianli/202312/zjianli2326.rar]
# * [HTTP/2] [1] [user-agent: curl/8.5.0]
# * [HTTP/2] [1] [accept: */*]
# } [5 bytes data]
# > GET /Files/DownLoad/jianli/202312/zjianli2326.rar HTTP/2
# > Host: downsc.chinaz.net
# > User-Agent: curl/8.5.0
# > Accept: */*
# >
# { [5 bytes data]
#   0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0< HTTP/2 200
# < content-type: application/octet-stream
# < last-modified: Wed, 27 Dec 2023 06:14:53 GMT
# < accept-ranges: bytes
# < etag: "fbef9a18c38da1:0"
# < server: Microsoft-IIS/10.0
# < x-powered-by: ASP.NET
# < date: Tue, 05 Aug 2025 02:52:15 GMT
# < content-length: 815828
# <
# { [16384 bytes data]
# 100  796k  100  796k    0     0   278k      0  0:00:02  0:00:02 --:--:--  278k
# * Connection #0 to host downsc.chinaz.net left intact    